<!DOCTYPE html>
<html>
  <head>
    <meta content="text/html; charset=windows-1252" http-equiv="content-type">
    <title>Chrono: Chronometer/stopwatch library</title>
    <link href="./style/style.css" media="screen" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="./style/prettify.js"></script>
	<script type="text/javascript" src="./scripts/toc.js"></script>
	<script type="text/javascript" src="./scripts/smoothscroll.js"></script>
    <script>

function handleLoad() {
 prettyPrint();
}

</script></head>
  <body onload="handleLoad();">
  
    <div id="container">
        <img style="margin-left: auto;margin-right: auto;display:block" src="images/chrono.png">
        <br>
      <h1>Chrono</h1>
      <p>Chronometer/stopwatch library that counts the time passed since started.</p>
	  <div id="generated-toc" class="generate_from_h2"></div>
      <h2>Get It!</h2>
      <ul>
        <li><a href="https://github.com/SofaPirate/Chrono/archive/master.zip"><strong>Download the source</strong></a></li>
        <li><a href="https://github.com/SofaPirate/Chrono/">Chrono on Github </a></li>
      </ul>
	  
      <h2>Adding Chrono</h2>
	   <p>Copy the <span class="hilite">Chrono</span> folder to your Arduino or Wiring <span class="hilite">libraries</span> folder <b>or</b> download it with the Arduino library manager.      
      <br>
      <h2>Basic Example</h2>
	  <pre class="prettyprint" ><code>
// INCLUDE CHRONO LIBRARY
// Documentation : https://github.com/SofaPirate/Chrono-Arduino-Wiring/blob/master/README.md
// Download : https://github.com/SofaPirate/Chrono-Arduino-Wiring/archive/master.zip
#include  &lt;Chrono.h&gt;

// Instanciate a Chrono object.
Chrono myChrono; 

void setup() {

}

void loop() {
  // Check whether the chronometer has reached 1000 time units.
  if (myChrono.hasPassed(1000)) {
    myChrono.restart(); // Restart the chronometer.

    // Do something here...
  }
}
</code></pre>
<h2>Basic Usage</h2>
<h3> Include the library </h3>

<pre class="prettyprint" ><code>// INCLUDE CHRONO LIBRARY : http://github.com/SofaPirate/Chrono
#include &lt;Chrono.h&gt;
</code></pre>

<h3> Create an instance </h3>
Once a Chrono is created, it starts counting (in milliseconds by default).

<pre class="prettyprint" ><code>
// CREATE A CHRONO INSTANCE :
Chrono myChrono;
</code></pre>

<h3> Basic Methods </h3>
<table>
            <tr>
                <td class="first">elapsed()</td>
                <td class="second"><span class="type number">unsigned long</span></td>
                <td> Returns the elapsed time</td>
            </tr>
			<tr>
                <td class="first">restart()</td>
                <td class="second"><span class="type void">void</span></td>
                <td>Sets the elapsed time to 0 and starts the chronometer</td>
            </tr>
             <tr>
                <td class="first">hasPassed( timeout )</td>
                <td class="second"><span class="type boolean">bool</span></td>
                <td>Returns true if the chronometer passed the timeout</td>
            </tr>
        </table>  

<h2>Example: Blinking the debug LED</h2>

<pre class="prettyprint" ><code>
/*
 This code will toggle pin 13 on and off every second (1000 ms).
 This should be the debug LED's pin. So the debug LED should
 blink every second.
 */

// INCLUDE CHRONO LIBRARY : http://github.com/SofaPirate/Chrono
#include <Chrono.h> 

// Set the led's pin
int ledPin =  13; 

//Create a variable to hold the led's state
int ledState = HIGH;

// Instanciate a Chrono object.
Chrono ledChrono; 

void setup()
{
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,ledState);
}

void loop()
{

  if (ledChrono.hasPassed(1000) ) { // returns true if it passed 1000 ms since it was started
    ledChrono.restart();  // restart the crono so that it triggers again later
    
    // toggle the stored state
    if (ledState==HIGH)  ledState=LOW;
    else ledState=HIGH;
    
    // write the state to the pin
    digitalWrite(ledPin,ledState);
  }
}
</code></pre>

<h2>Example: Blinking 2 LEDS</h2>
<img src="images/LED_on_pin_9.png">
<pre class="prettyprint" ><code>
// This code will toggle output 13 every 250 ms
// and will toggle output 9 every 125 ms


// INCLUDE CHRONO LIBRARY : http://github.com/SofaPirate/Chrono
#include <Chrono.h> 

// Create variables for the LED pins
int ledPinA =  13;
int ledPinB =  9;

// Create variables to hold the LED states
int ledStateA = HIGH;
int ledStateB = HIGH;

// Instantiate two Chronos
Chrono chronoA; 
Chrono chronoB; 

void setup()
{
  pinMode(ledPinA,OUTPUT);
  digitalWrite(ledPinA,ledStateA);
  
   pinMode(ledPinB,OUTPUT);
  digitalWrite(ledPinB,ledStateB);
  
}

void loop()
{
  // Use Chrono as a metronome with an interval of 250 ms: 
  if ( chronoA.hasPassed(250) ) { // returns true if it passed 250 ms since it was started
    chronoA.restart(); // restart the crono so that it triggers again later
    ledStateA = !ledStateA; // !: toggle the state from 0 to 1 or from 1 to 0
    digitalWrite(ledPinA,ledStateA);
  }
  
  // Use Chrono as a metronome with an interval of 125 ms: 
   if ( chronoB.hasPassed(125) ) { // returns true if it passed 125 ms since it was started
    chronoB.restart(); // restart the crono so that it triggers again later
    ledStateB = !ledStateB; // !: toggle the state from 0 to 1 or from 1 to 0
    digitalWrite(ledPinB,ledStateB);
  }
  
  
}
</code></pre>


<h2>Advanced Usage</h2>


<h3> Time units </h3>
You can create a Chrono that counts in microseconds or seconds:
<pre class="prettyprint" ><code>Chrono myChronoMicros(Chrono::MICROS);
</code></pre>
<pre class="prettyprint" ><code>Chrono myChronoSeconds(Chrono::SECONDS);
</code></pre>

<h3> Custom time function </h3>
Alternatively you can create a Chrono with a custom time function:
<pre class="prettyprint" ><code>unsigned long mySpecialTimeFunction();

Chrono myChronoMicros(mySpecialTimeFunction);
</code></pre>

<h3>Advanced Methods</h3>
<table>
            <tr>
                <td class="first">restart( offset )</td>
                <td class="second"><span class="type void">void</span></td>
                <td>You can alternatively start(restart) the chronometer with a time offset</td>
            </tr>
			<tr>
                <td class="first">stop()</td>
                <td class="second"><span class="type void">void</span></td>
                <td>Stops/pauses the chronometer</td>
            </tr>
             <tr>
                <td class="first">resume( )</td>
                <td class="second"><span class="type void">void</span></td>
                <td>Resumes the chronometer</td>
            </tr>
			<tr>
                <td class="first">hasPassed( timeout,restartIfPassed )</td>
                <td class="second"><span class="type boolean">bool</span></td>
                <td>If the chronometer passed the timeout, returns true and restarts the chronometer </td>
            </tr>
			<tr>
                <td class="first">add( time )</td>
                <td class="second"><span class="type void">void</span></td>
                <td>Immediately adds some time to the chronometer</td>
            </tr>
			<tr>
                <td class="first">isRunning( )</td>
                <td class="second"><span class="type boolean">bool</span></td>
                <td>Returns true if the chronometer is currently running</td>
            </tr>
			<tr>
                <td class="first">delay( time )</td>
                <td class="second"><span class="type void">void</span></td>
                <td>Waits for some time (in the time unit of the chronometer)</td>
            </tr>
			
        </table>  

<br> 
    </div>
	<br>
	<br>
  </body>
</html>
